package libra;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.csp.sentinel.slots.block.BlockException;

public class SentinelResourceHandler {
	// blockExec函数，原方法调用被限流/降级/系统保护的时候调用
	public static Object blockExec(String resoucerName, int i, HttpServletResponse resp, BlockException ex) throws Exception {
		resp.setStatus(429);
		// 资源访问阻止，被限流或被降级
		System.err.println( resoucerName + "限流异常...i:" + i + "...Ex:" + ex.getClass().getName() );
		return resoucerName + "限流异常...i:" + i + "...Ex:" + ex.getClass().getName();
	}
	// fallbackExec函数，原方法抛出异常的时候提供fallback处理逻辑
	public static Object fallbackExec(String resoucerName, int i, HttpServletResponse resp, Throwable throwable) throws Exception {
		resp.setStatus(500);
		// 资源访问阻止，被限流或被降级
		System.err.println( resoucerName + "执行异常...i:" + i + "...Throwable:" + throwable.getClass().getName() );
		return resoucerName + "执行异常...i:" + i + "...Throwable:" + throwable.getClass().getName();
	}
}
